//
//  TableChooseViewController.m
//  Model
//
//  Created by wangchonggang on 16/9/18.
//  Copyright © 2016年 wcg. All rights reserved.
//

#import "TableChooseViewController.h"

#import "ChooseTableViewCell.h"


static NSString *indexStr;
static NSString *value;
@interface TableChooseViewController ()<UITableViewDelegate, UITableViewDataSource>

@end

@implementation TableChooseViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.IdArr = [[NSMutableArray alloc] init];
        self.SelectDic = [NSMutableDictionary dictionary];
        self.Type = 2;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.view setBackgroundColor:[UIColor whiteColor]];
    
    [self TableViewAdd];
    [self RightAdd];
}

- (void)RightAdd {
    UIButton *Select = [UIButton buttonWithType:UIButtonTypeCustom];
    [Select setFrame:CGRectMake(0, 0, 100, 100)];
    [Select setTitle:@"完成" forState:UIControlStateNormal];
    [Select addTarget:self action:@selector(Submit) forControlEvents:UIControlEventTouchUpInside];
    [Select setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:Select];
}

- (void)Submit {


    NSArray *arr = [_MainTable visibleCells];
    [_SelectDic removeAllObjects];
    for (ChooseTableViewCell *cell in arr) {
        if (CGColorEqualToColor(cell.SelectStatus.backgroundColor.CGColor, [UIColor redColor].CGColor)) {
            [_SelectDic setValue:[NSString stringWithFormat:@"model%ld", cell.SelectStatus.tag] forKey:[NSString stringWithFormat:@"%ld", cell.SelectStatus.tag - 100]];
        }
        else {

        }
    }
    NSLog(@"%@", _SelectDic);
    
}

- (void)TableViewAdd {
    self.MainTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
    [_MainTable registerClass:[ChooseTableViewCell class] forCellReuseIdentifier:@"aaa"];
    _MainTable.delegate = self;
    _MainTable.dataSource = self;
    [_MainTable setTableFooterView:[[UIView alloc] init]];
    [self.view addSubview:_MainTable];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 10;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ChooseTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"aaa" forIndexPath:indexPath];
    [cell.SelectStatus setTag:100 + indexPath.row];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return self.view.frame.size.height / 10;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    ChooseTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    
    if (_Type == 1) {
        
    }
    else if (_Type == 2) {
        NSArray *arr = [_MainTable visibleCells];
        for (ChooseTableViewCell *cell in arr) {
            [cell.SelectStatus setBackgroundColor:[UIColor lightGrayColor]];
        }

    }
    
    if (CGColorEqualToColor(cell.SelectStatus.backgroundColor.CGColor, [UIColor lightGrayColor].CGColor)) {
        [cell.SelectStatus setBackgroundColor:[UIColor redColor]];
    }
    else {
        [cell.SelectStatus setBackgroundColor:[UIColor lightGrayColor]];
    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
